/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.webhomesites.configuracao.presenter;

import br.com.webhomesites.configuracao.contracts.IUsuarioPresenter;
import br.com.webhomesites.configuracao.contracts.IUsuarioView;
import br.com.webhomesites.configuracao.model.UsuarioModel;
import br.com.webhomesites.configuracao.presenter.UsuarioPresenterActionListeners.*;
import br.com.webhomesites.padrao.utils.Strategy;
import br.com.webhomesites.configuracao.view.UsuarioView;
import br.com.webhomesites.configuracao.utils.ConfiguracaoInstancias;
import java.awt.Desktop;
import java.util.List;

/**
 *
 * @author Webhome
 */
public class UsuarioPresenter implements IUsuarioPresenter {

    private UsuarioModel model;  
    private IUsuarioView view;    
    
    public final Strategy INSERT_STRATEGY = new InsertStrategy();  
    public final Strategy UPDATE_STRATEGY = new UpdateStrategy();  
    
    private Strategy operacao = INSERT_STRATEGY;
    
    public UsuarioPresenter(UsuarioModel model, IUsuarioView view) {
        this.model = model;
        this.view = view;
    }
    
    @Override
    public void setUpViewListeners() {
        view.setInserirActionListener(new InserirActionListener(this));  
        view.setRemoverActionListener(new RemoverActionListener(this));  
        view.setAlterarActionListener(new AlterarActionListener(this));  
        view.setCancelarActionListener(new CancelarActionListener(this));  
        view.setSalvarActionListener(new SalvarActionListener(this));  
        view.setBtnSairActionListener(new SairActionListener());
    }

    @Override
    public void updateModelFromView() {
        model.setCodigo(view.getCodigo());  
        model.setNome(view.getNome());  
        model.setLogin(view.getLogin());
        model.setSenha(view.getSenha());        
    }

    @Override
    public void updateViewFromModel() {
        if (model != null) {  
            view.setCodigo(model.getCodigo());  
            view.setNome(model.getNome());  
            view.setLogin(model.getLogin());  
            view.setSenha(model.getSenha());               
        }  
        else {  
            view.clearFields();  
        }
    }

    @Override
    public void novoUsuario() {
        model = new UsuarioModel();
    }

    @Override
    public void inserirUsuario() {
        this.updateModelFromView(); 
    }

    @Override
    public void alterarUsuario() {
        this.updateModelFromView();
    }

    @Override
    public void removerUsuario() {
         this.updateModelFromView();
    }

    @Override
    public void habilitarEdicao(boolean arg) {
        view.enableTxtUsuarioCodigo(arg);  
        view.enableTxtUsuarioNome(arg);  
        view.enableTxtUsuarioLogin(arg);  
        view.enableTxtUsuarioSenha(arg);
        
        view.enableBtnCancelar(arg);
        view.enableBtnRemover(arg);
        view.enableBtnSalvar(arg);        
    }

    @Override
    public IUsuarioView getView() {
        return view;
    }
    
    @Override
    public UsuarioModel getModel() {
        return model;
    }
    
    @Override
    public void setView(IUsuarioView view) {
        this.view = view;
    }

    @Override
    public void setModel(UsuarioModel model) {
        this.model = model;
    }

    @Override
    public void createView() {
        this.novoUsuario();            
        this.setUpViewListeners();  
        this.habilitarEdicao(false);  
        
        view.packAndShow();
    }
   
    public void setOperacao(Strategy operacao) {
        this.operacao = operacao;
    }

    public Strategy getOperacao() {
        return operacao;
    }

     private class InsertStrategy implements Strategy {  
        @Override
        public void execute() {  
            inserirUsuario();  
        }  
    }        
      
    private class UpdateStrategy implements Strategy {  
        @Override
        public void execute() {  
            alterarUsuario();  
        }  
    }        
    
}
